﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;
using E.Enum;

using X.Common;

namespace E.Web.Teacher.internals.publics.course
{
    public partial class Edit : TeacherBasePage
    {
        public int id;
        public CourseModel model = new CourseModel();
        public Dictionary<int, string> projectList;
        public string act;

        protected override void ShowPage()
        {
            CourseBLL bll = new CourseBLL();
            projectList = bll.GetInternalPublicProjectList();

            id = XRequest.GetInt("id", -1);
            act = XRequest.GetString("act");

            if (ispost)
            {
                string name = XRequest.GetString("NameTxt");
                decimal schoolHour = XRequest.GetString("SchoolHourTxt").Equals("") ? 0 : Convert.ToDecimal(XRequest.GetString("SchoolHourTxt"));
                string description = XRequest.GetString("DescriptionTxt");

                if (CheckValue())
                {
                    model = new CourseModel();
                    model.Name = name;
                    model.SchoolHour = schoolHour;
                    model.Description = description;

                    int rst = 0;
                    string href = "";

                    switch (act)
                    {
                        case "n":
                            bool needLearn = XRequest.GetInt("NeedLearnRg", -1) == 1 ? true : false;
                            int projectid = XRequest.GetInt("ProjectSlt", -1);

                            model.CategoryID = -1;
                            model.IsPublic = false;
                            model.CnPID = projectid;
                            model.NeedLearn = needLearn;
                            model.IsInternalPublic = true;

                            rst = bll.AddInternalCourse(model);
                            href = "/internals/course/Edit.aspx?act=n";

                            break;
                        case "u":
                            model.ID = id;

                            rst = bll.UpdateInternalCourse(model);

                            href = "/internals/course/Edit.aspx?act=u&id=" + id;

                            break;
                        default: break;
                    }

                    if (rst > 0)
                    {
                        ExecuteJS("alert('操作成功'); window.location = '" + href + "';");
                    }
                    else
                    {
                        Alert("操作失败");
                    }
                }
            }
            else
            {
                if (id != -1)
                {
                    model = bll.GetInternalCourseInfoById(id);
                    if (model.ID == -1)
                    {
                        ExecuteJS("alert('数据读取错误'); window.location = '/internals/course/List.aspx';");
                    }
                }
            }
        }

        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("NameTxt").Equals(""))
            {
                Alert("课程名称不能为空");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && XRequest.GetInt("ProjectSlt", -1) == -1)
            {
                Alert("请选择所属项目组");
                rst = false;
            }

            if (XRequest.GetString("act") == "n" && XRequest.GetInt("NeedLearnRg", -1) == -1)
            {
                Alert("请选择该课程是否需要在线上课");
                rst = false;
            }

            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
